package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.WaContactUser;
import com.ruoyi.system.service.WaContactUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wa/user")
public class WaContactUserController extends BaseController {
    @Autowired
    private WaContactUserService contactUserService;

    /**
     * 新增联系人
     * @param waContactUser
     * @return
     */
    @PostMapping
    public AjaxResult addUser(@RequestBody WaContactUser waContactUser) {
        int result = contactUserService.addUserInfo(waContactUser);
        if (result > 0) {
            return AjaxResult.success("添加成功");
        } else if (result == -1) {
            return AjaxResult.error("手机号已被注册");
        } else {
            return AjaxResult.error("添加失败");
        }
    }


    /**
     * 删除联系人信息 逻辑删除  在库中依然存在
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public AjaxResult removeUser(@PathVariable long userId)
    {
        return toAjax(contactUserService.deleteUserById(userId));
    }
    /**
     * 编辑联系人
     */
    @PutMapping
    public AjaxResult editUser(@RequestBody WaContactUser contactUser) {
        contactUser.setUpdateBy(SecurityUtils.getUsername());
        int result = contactUserService.editUser(contactUser);
        if (result > 0) {
            return AjaxResult.success("修改成功");
        } else if (result == -1) {
            return AjaxResult.error("手机号已被其他用户使用");
        } else {
            return AjaxResult.error("修改失败");
        }
    }
}
